package com.rabbit.eap.framework.config;

import com.rabbit.eap.config.properties.EapFilterProperties;
import com.rabbit.eap.config.properties.EapInterceptorProperties;
import com.rabbit.eap.config.properties.EapProperties;
import com.rabbit.eap.framework.core.filter.RequestDetailFilter;
import com.rabbit.eap.framework.core.interceptor.PermissionInterceptor;
import com.rabbit.eap.framework.core.interceptor.ResourceInterceptor;
import com.rabbit.eap.framework.core.xss.XssFilter;
import com.rabbit.eap.framework.utils.IniUtil;
import com.rabbit.eap.framework.utils.JsonUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.PostConstruct;
import java.util.Map;

/**
 * @projectName eap
 * @packageName com.rabbit.eap.config
 * @className EapWebMvcConfig
 * @author： rabbit
 * @version： 1.0
 * @since： 2020/4/27 21:47
 * <p>Description: EapWebMvcConfig </p>

 */
@Slf4j
@Configuration
public class EapWebMvcConfig implements WebMvcConfigurer {

    /**    * eap配置属性
     */
    @Autowired
    private EapProperties eapProperties;

    /**
     * Filter配置
     */
    private EapFilterProperties filterConfig;

    /**
     * 拦截器配置
     */
    private EapInterceptorProperties interceptorConfig;

    /**
     * RequestDetailFilter配置
     *
     * @return
     */
    @Bean
    public FilterRegistrationBean requestDetailFilter() {
        EapFilterProperties.FilterConfig requestFilterConfig = filterConfig.getRequest();
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        filterRegistrationBean.setFilter(new RequestDetailFilter());
        filterRegistrationBean.setEnabled(requestFilterConfig.isEnable());
        filterRegistrationBean.addUrlPatterns(requestFilterConfig.getUrlPatterns());
        filterRegistrationBean.setOrder(requestFilterConfig.getOrder());
        filterRegistrationBean.setAsyncSupported(requestFilterConfig.isAsync());
        return filterRegistrationBean;
    }

    /**
     * XssFilter配置
     *
     * @return
     */
    @Bean
    public FilterRegistrationBean xssFilter() {
        EapFilterProperties.FilterConfig xssFilterConfig = filterConfig.getXss();
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        filterRegistrationBean.setFilter(new XssFilter());
        filterRegistrationBean.setEnabled(xssFilterConfig.isEnable());
        filterRegistrationBean.addUrlPatterns(xssFilterConfig.getUrlPatterns());
        filterRegistrationBean.setOrder(xssFilterConfig.getOrder());
        filterRegistrationBean.setAsyncSupported(xssFilterConfig.isAsync());
        return filterRegistrationBean;
    }


    /**
     * 自定义权限拦截器
     *
     * @return
     */
    @Bean
    public PermissionInterceptor permissionInterceptor() {
        return new PermissionInterceptor();
    }


    /**
     * 资源拦截器
     *
     * @return
     */
    @Bean
    public ResourceInterceptor resourceInterceptor() {
        return new ResourceInterceptor();
    }



    @PostConstruct
    public void init() {
        filterConfig = eapProperties.getFilter();
        interceptorConfig = eapProperties.getInterceptor();
        // 打印EapProperties配置信息
        log.debug("EapProperties：{}", JsonUtils.toJSONString(eapProperties));
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {

        // 资源拦截器注册
        if (interceptorConfig.getResource().isEnable()) {
            registry.addInterceptor(resourceInterceptor())
                    .addPathPatterns(interceptorConfig.getResource().getIncludePaths());
        }

        // 自定义权限拦截器注册
        if (interceptorConfig.getPermission().isEnable()) {
            registry.addInterceptor(permissionInterceptor())
                    .addPathPatterns(interceptorConfig.getPermission().getIncludePaths())
                    .excludePathPatterns(interceptorConfig.getPermission().getExcludePaths());
        }

    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 设置项目静态资源访问
        String resourceHandlers = eapProperties.getResourceHandlers();
        if (StringUtils.isNotBlank(resourceHandlers)) {
            Map<String, String> map = IniUtil.parseIni(resourceHandlers);
            for (Map.Entry<String, String> entry : map.entrySet()) {
                String pathPatterns = entry.getKey();
                String resourceLocations = entry.getValue();
                registry.addResourceHandler(pathPatterns)
                        .addResourceLocations(resourceLocations);
            }
        }

        // 设置上传文件访问路径
        registry.addResourceHandler(eapProperties.getResourceAccessPatterns())
                .addResourceLocations("file:" + eapProperties.getUploadPath());
    }


}
